/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ourMessage;

import ChateoController.Controller;
import ChateoFacadeNI.FacadeNI;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author abdala
 */
public class ConnexionUDPServer extends Thread {
    private DatagramSocket socketServ;
    private FacadeNI facada;
    private final int DATA_SIZE=1000;
    //As the variable is static we CAN'T create multiple servers from one thread main thread.
    private static int portServ=2014;
    //OBSELETE
    /*public ConnexionUDPServer() throws SocketException{
        //Create a UDP socket linked to the server's port
        socketServ = new DatagramSocket(portServ);  
        //Starting the reception Thread
        super.start();
    }
    */
    
    public ConnexionUDPServer(int portServ,FacadeNI facada){
        this.portServ=portServ;
        this.facada=facada;
        //Create a UDP socket linked to the server's port
        System.out.println("In " + ConnexionUDPServer.class.getName() + " : Creating the server socket on " + portServ);
        try {  
            this.socketServ = new DatagramSocket(portServ);
        } catch (SocketException ex) {
            System.out.println("Probleme lors de la creation du Serveur");
            Logger.getLogger(ConnexionUDPServer.class.getName()).log(Level.SEVERE, null, ex);
        }
        //Starting the reception Thread
        super.start();
    }
    
    public void run() {
        boolean active=true;
        String currentLogin;
        if(facada.getTheController().DebugMode()){
        System.out.println("Server Thread Started");
        }
        while(active){
            try{
                //An array for data reception
                byte[] data = new byte[DATA_SIZE];
        
                //Creation of a packet using the data array
                DatagramPacket packet = new DatagramPacket(data, data.length);
                
                
                //Waiting for a packet
                socketServ.receive(packet);
                if(facada.getTheController().DebugMode()){
                     System.out.println("A Packet has been received");
                }
                
                //Getting IP Adress of the current client
                InetAddress ClientIP = packet.getAddress();
                
                
                //Displaying data received
                //String car = new String(packet.getData(),0,packet.getLength());
                Message msg = Message.fromArray(data);
                
                if(msg instanceof Hello) {
                    
                    Hello hello = (Hello)msg;
                    currentLogin=hello.getlogin();
                     if (hello.isAck())
                     {
                         facada.incommingAck(currentLogin, ClientIP);
                     }
                     else
                     {
                         facada.incommingHello(currentLogin, ClientIP);
                     }
                }
                else if (msg instanceof Bye) {
                    Bye bye = (Bye)msg;
                    currentLogin=msg.getlogin();
                    if (Controller.DebugMode()){System.out.println("Login : "+ currentLogin);};
                    facada.incommingBye(currentLogin, ClientIP);
                    
                    
                }
                else if (msg instanceof Text) {
                    Text text = (Text)msg;
                    currentLogin=text.getlogin();
                    facada.incommingText(ClientIP,currentLogin,text.getText(),text.isPrivate());
                    
                    
                }
                else {
                //WHAT IS THIS MESSAGE ?!
                    System.out.println("ERROR : What is this message ?");
                }
                
                System.out.println("The server has received : " +msg.toString());
            }
            catch(Exception e){
                active = false;
                e.printStackTrace();
            }
        }
        
    }
    
}